package com.msb.servicemap.remote;

import com.msb.internalcommon.constant.AmapConfigConstants;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.reponese.DirectionResponse;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: jiudianliu
 * @Date: 2023/6/2 - 06 - 02 - 17:07
 * @Description: com.msb.servicemap.remote
 * @version: 1.0
 */

@Service
@Slf4j
public class MapDirectionClient {


    //导入yml文件中的key
    @Value("${map.key}")
    private String amapKey ;

    @Autowired
    private RestTemplate restTemplate ;


    public DirectionResponse direction(String depLongitude , String depLatitude , String destLongitude , String destLatitude ){

        //组装请求调用url
        /**
         * https://restapi.amap.com/v3/direction/driving?origin=116.481028,39.989643&destination=116.465302,40.004717&extensions=all&output=json&key=e3622176f7f4203f24665a5df8289f57
         */
        StringBuilder urlBuild = new StringBuilder() ;
        urlBuild.append(AmapConfigConstants.DIRECTION_URL) ;
        urlBuild.append("?") ;
        urlBuild.append("origin=" + depLongitude + "," + depLatitude) ;
        urlBuild.append("&") ;
        urlBuild.append("destination=" + destLongitude + "," + destLatitude) ;
        urlBuild.append("&") ;
        urlBuild.append("extensions=base") ;
        urlBuild.append("&") ;
        urlBuild.append("output=json") ;
        urlBuild.append("&") ;
        urlBuild.append("key=" + amapKey) ;
        log.info(urlBuild.toString()) ;

        //调用高德接口
        ResponseEntity<String> directionEntity = restTemplate.getForEntity(urlBuild.toString(), String.class);
        String directionEntityBody = directionEntity.getBody();
        log.info("高德地图路径规划返回信息：" + directionEntity.getBody());
        //解析接口
        DirectionResponse directionResponse = parseDirectionEntity(directionEntityBody);

        return directionResponse ;
    }

    /**\
     * 获取Json格式中的distance和duration 字段
     * @param directionEntityBod
     * @return
     */
    private DirectionResponse parseDirectionEntity(String directionEntityBod){
        DirectionResponse directionResponse = null ;
        try{
            //新建directionResponse对象
            directionResponse = new DirectionResponse();
            //将字符串转换成Json格式
            JSONObject result = JSONObject.fromObject(directionEntityBod);
            //如果有status字段
            if (result.has(AmapConfigConstants.STATUS)) {
                //获取status字段
                int status = result.getInt(AmapConfigConstants.STATUS);
                //如果status == 1  那么就说成功了，就读取里边的数据
                if (status == 1) {
                    if (result.has(AmapConfigConstants.ROUTE)) {
                        //挨个往下获取
                        JSONObject routeObject = result.getJSONObject(AmapConfigConstants.ROUTE);
                        JSONArray pathsArray = routeObject.getJSONArray(AmapConfigConstants.PATHS);
                        JSONObject pathObject = pathsArray.getJSONObject(0);
                        directionResponse = new DirectionResponse() ;
                        // 直到获取到paths下面的distance和duration 字段
                        if (pathObject.has(AmapConfigConstants.DISTANCE)){
                            int distance = pathObject.getInt(AmapConfigConstants.DISTANCE);
                            directionResponse.setDistance(distance) ;
                        }
                        if (pathObject.has(AmapConfigConstants.DURATION)){
                            int duration = pathObject.getInt(AmapConfigConstants.DURATION);
                            directionResponse.setDuration(duration); ;
                        }
                    }
                }
            }
        }catch (Exception e){

        }

        return directionResponse ;
    }
}
